using System;

using Impact.Model.Objects;
using Impact.Web.Foundation;
using System.Web.UI;
using Impact.Infrastructure.Exceptions;
using Impact.Model.Service;
using StructureMap;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Impact.Web.Controls
{
    public partial class DisplayPlugins : Impact.Web.Foundation.UserControlBase
    {

        /// <summary>
        /// loads the plugins that will be displayed
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (page != null)
            {

                IPluginService pluginService = ObjectFactory.GetInstance<IPluginService>();
                PagePluginCollection plugins = pluginService.FetchByPageByLocation(page.PageId, location);

                plugins.GetList().ForEach(delegate(PagePlugin pluginReg)
                {
                    // request the plugin to Render the plugin based on the parameters configured
                    // on the admin screen

                    //WebControl container = new WebControl(HtmlTextWriterTag.Div);
                    //this.Controls.Add(container);

                    pluginReg.Plugin.Render(this, pluginReg.GetParameters());
                });

            }

        }

        /// <summary>
        /// gets or sets the location that this control will display plugins for
        /// </summary>
        public int Location
        {
            get { return location; }
            set { location = value; }
        }
        private int location;

        /// <summary>
        /// the page to retrieve plugins for
        /// </summary>
        public ImpactPage PageData
        {
            get { return page; }
            set { page = value; }
        }
        private ImpactPage page;

        /// <summary>
        /// textual description of this location
        /// </summary>
        public string LocationDescription
        {
            get { return description ?? this.Location.ToString(); }
            set { description = value; }
        }
        private string description;



    }
}